import java.io.UnsupportedEncodingException;

/**
 * @author xxx
 * @ClassName: StringUtils
 */
public class StringUtils {

    /**
     * 定义字符切割的最大字节数
     */
    private static int spiltSize = 22000;
    private static String spiltString(String resource, int index) {
        if (resource == null)
            return "String is null";
        byte[] strByte = resource.getBytes();
        int byteLength = strByte.length;
        if (index <= 0 || index > byteLength)
            return "out of index for this string";
        int strLength = resource.length();
        String[] tmpByte = new String[strLength];
        for (int i = 0; i < strLength; i++) {
            tmpByte[i] = resource.substring(i, i + 1);
        }
        int myIndex = getIndex(tmpByte, strLength, index);
        byte[] resultByte = getBytesByIndex(tmpByte, myIndex);
        if (resultByte == null)
            return "error";
        String result = new String(resultByte);
        return result;
    }

    private static int getIndex(String[] tmpByte, int length, int index) {
        int byteTotleMin = 0;
        int myIndex = -1;
        for (int i = 0; i < length; i++) {
            byteTotleMin = byteTotleMin + tmpByte[i].getBytes().length;
            if (byteTotleMin >= index) {
                myIndex = i;
                break;
            }
        }
        return myIndex;
    }

    private static byte[] getBytesByIndex(String[] tmpByte, int index) {
        if (index > tmpByte.length)
            return null;
        index++;
        byte[][] tmp = new byte[index][];
        int count = 0;
        for (int i = 0; i < index; i++) {
            tmp[i] = tmpByte[i].getBytes();
            count = count + tmp[i].length;
        }
        byte[] result = new byte[count];
        int one = tmp.length;
        int indexTmp = 0;
        for (int i = 0; i < one; i++) {
            int two = tmp[i].length;
            for (int j = 0; j < two; j++) {
                result[indexTmp] = tmp[i][j];
                indexTmp++;
            }
        }
        return result;
    }

    public static String[] getSpiltMessage(String messageContent)
            throws UnsupportedEncodingException {
        String message = messageContent;
        int byteLen = message.getBytes("UTF-8").length;
        int len = byteLen / spiltSize + 1;
        String[] strArr = new String[len];
        if (byteLen > spiltSize) {
            for (int i = 0; i < len; i++) {
                StringBuffer strTemp = new StringBuffer();
                for (int j = 0; j < i; j++) {
                    if (!"null".equals(strArr[j].toString())) {
                        strTemp.append(strArr[j]);
                    }
                }
                int start = message.indexOf(strTemp.toString());
                if (start == -1) {
                    start = 0;
                }
                // 对null情况做特殊处理
                int start2 = strTemp.toString().length();
                if (start2 == 4) {
                    start2 = 0;
                }
                String subTemp = "";
                if (start + start2 == 0) {
                    subTemp = message;
                } else {
                    // 找到切割点
                    subTemp = message.substring(start + start2);
                }
                if (subTemp.getBytes().length > spiltSize) {
                    strArr[i] = spiltString(subTemp, spiltSize);
                    // 再次检测一下字节大小,保证要小于256字节
                    if (strArr[i].getBytes("UTF-8").length > spiltSize) {
                        strArr[i] = strArr[i].substring(0,
                                strArr[i].length() - 9);
                    }
                }else{
                    strArr[i] = subTemp;
                }
            }

        } else {
            strArr[0] = message;
        }
        // 补全256位
        for (int k = 0; k < len; k++) {
            if (null != strArr[k]
                    && strArr[k].getBytes("UTF-8").length < spiltSize) {
                StringBuffer strnull = new StringBuffer();
                for (int y = 0; y < spiltSize
                        - strArr[k].getBytes("UTF-8").length; y++) {
                    strnull.append(" ");
                }
                strArr[k] = strArr[k] + strnull.toString();
            }
        }
        return strArr;
    }

}
